<template>
    <div class="tools-container">
        <a-row>
            <a-col class="mrt">
                <a-space>
                    <a-select :style="{ width: '310px' }" v-model="backuptServeData" placeholder="选择备份的的服务" multiple
                        allow-clear>
                        <a-option v-for="(item, index) in serves">{{ item }}</a-option>
                    </a-select>
                    <a-button type="primary" :loading="isBackuped" @click="backupServe">
                        <template #icon>
                            <icon-save />
                        </template>
                        <template #default>开始备份</template>
                    </a-button>
                </a-space>
            </a-col>
        </a-row>
    </div>
</template>

<script>
import { IconSave } from "@arco-design/web-vue/es/icon";
import { fileBackup ,readServerFile} from "@/api/index.js";
export default {
    components: {
        IconSave
    },
    data() {
        return {
            serveData:[],
            backuptServeData: [],
            serves: [
                "lib",
                "lcpt-gateway2-host",
                "lcpt-fina-pub-online",
                "lcpt-fina-pub-api",
                "lcpt-fina-pub-batch",
                "lcpt-fina-online-trade",
                "lcpt-fina-online-query",
                "lcpt-web-manager-fina",
                "lcpt-register",
                "lcpt-fina-batch1",
                "lcpt-fina-batch2",
                "lcpt-schedule",
                "lcpt-fina-api",
                "lcpt-gateway2-tcpcomm-bank",
                "lcpt-datax",
                "lcpt-gateway2-bank",
                "lcpt-dxfund-online-trade",
                "lcpt-web-manager-dxfund",
                "lcpt-dxfund-pub-batch",
                "lxpt-dxfund-pub-online",
                "lcpt-dxfund-pub-api",
                "lcpt-dxfund-batch1",
                "lcpt-dxfund-batch2",
                "lcpt-dxfund-online-query",
                "lcpt-dxfund-api",
                "lcpt-web-manager-insure",
                "lcpt-insure-pub-online",
                "lcpt-insure-pub-batch",
                "lcpt-insure-pub-api",
                "lcpt-insure-online",
                "lcpt-insure-batch1",
                "lcpt-insure-batch2",
                "lcpt-insure-api",
            ],
            isBackuped: false,

        }
    },
    methods: {
        backupServe() {
            // console.log(this.genServerListIp());
            let backServerListIp = this.genServerListIp();
            fileBackup({
                serverList: backServerListIp,
                type: "back"
            })
                .then((res) => {
                    console.log("备份文件==");
                    console.log(res);
                    console.log("备份文件==");
                    if (res.status == 200) {
                        this.$toast.open({
                            message: res.data,
                            type: "success",
                            duration: 3000,
                            position: "top-right",
                        });
                        return true;
                    }
                })
                .catch((error) => {
                    this.$toast.open({
                        message: "失败，请检查控制台日志！",
                        type: "error",
                        duration: 3000,
                        position: "top-right",
                    });
                    console.log(error);
                })
        },
        genServerIp(server) {
            let result = [];
            server.forEach((element) => {
                let ip = element.ip;
                let serverPath = element.serverPath;
                let username = element.username;
                let port = element.port;
                let password = element.password;
                element.server.forEach((e) => {
                    let obj = {
                        serverName: e,
                        ip: ip,
                        serverPath: serverPath,
                        username: username,
                        port: port,
                        password: password,
                    };
                    result.push(obj);
                });
            });
            return result;
        },
        genServerListIp() {
            let restData = JSON.parse(JSON.stringify(this.backuptServeData));
            let serData = JSON.parse(JSON.stringify(this.serveData));
            let serverIP = this.genServerIp(serData);
            // console.log(restData);
            // console.log(serData);
            let pack = [];
            restData.forEach((file) => {
                let fileName = file;
                // 普通服务的遍历
                serverIP.forEach((server) => {
                    // console.log(server);
                    if (fileName == server.serverName) {
                        let obj = {
                            ip: server.ip,
                            serverName: fileName,
                            serverPath: server.serverPath,
                            username: server.username,
                            port: server.port,
                            password: server.password,
                        };
                        pack.push(obj);
                    }
                });
            });
            return pack;
        },
    },
    mounted() {
        // 远程读取server-json
    readServerFile({
      fileName: "server.json",
    })
      .then((res) => {
        this.serveData = res.data.data;
        this.$toast.open({
          message: "服务器配置文件读取成功",
          type: "success",
          duration: 3000,
          position: "top-right",
        });
      })
      .catch((error) => {
        // console.log(error);
        this.$toast.open({
          message: "该功能需要启动后台服务，请检查是否启用后台服务！",
          type: "error",
          duration: 6000,
          position: "top-right",
        });
      });
    },
}
</script>